package com.efuture.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNAME JdbcQueryAll
 * @Description 查询emp表 返回一个emp对象的集合
 * @Author zhaoshanshan
 * @Date 2021/9/19 7:41 下午
 * @Version 1.0
 */
public class JdbcQueryAll {

    public static void main(String[] args) {
        List<Emp> empData =  findAll();
        if(null!=empData&&empData.size()>0){
            for(Emp emp:empData){
                System.out.println(emp.toString());
            }
        }
    }
    public static List<Emp> findAll(){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rst = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3","root","zhaoss");
            String sql = "select * from emp";
            stmt = conn.createStatement();
            rst = stmt.executeQuery(sql);
            if(null!=rst){
                Emp emp = null;
               List<Emp> empList = new ArrayList<>();
                while (rst.next()){
                   int id =  rst.getInt("id");
                   String eName = rst.getString("ename");
                   int jobId =  rst.getInt("job_id");
                   int mgr = rst.getInt("mgr");
                   Date joinDate = rst.getDate("joindate");
                   double salary = rst.getDouble("salary");
                   double bonus = rst.getDouble("bonus");
                   int deptId = rst.getInt("dept_id");
                    emp = new Emp();
                    emp.setId(id);
                    emp.seteName(eName);
                    emp.setJobId(jobId);
                    emp.setMgr(mgr);
                    emp.setJoinDate(joinDate);
                    emp.setSalary(salary);
                    emp.setBonus(bonus);
                    emp.setDeptId(deptId);
                    empList.add(emp);
                }
                return empList;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try{
                if(null!=conn){
                    conn.close();
                }
                if(null!=stmt){
                    stmt.close();
                }
                if(null!=rst){
                    rst.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    };
}
class Emp{
    private int id;
    private String eName;
    private int jobId;
    private int mgr;
    private Date joinDate;
    private double salary;
    private double bonus;
    private int deptId;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }



    public int getJobId() {
        return jobId;
    }

    public void setJobId(int jobId) {
        this.jobId = jobId;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public String geteName() {
        return eName;
    }

    public void seteName(String eName) {
        this.eName = eName;
    }

    public Date getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    public int getDeptId() {
        return deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", eName='" + eName + '\'' +
                ", jobId=" + jobId +
                ", mgr=" + mgr +
                ", joinDate=" + joinDate +
                ", salary=" + salary +
                ", bonus=" + bonus +
                ", deptId=" + deptId +
                '}';
    }
}